package com.antoco.operation.bigscreen.controller.modelcontroller.impl;

import com.antoco.operation.bigscreen.controller.modelcontroller.intf.SuiziSensorNumController;
import com.antoco.operation.bigscreen.model.dto.SuiziSensorNumDTO;
import com.antoco.operation.bigscreen.model.entity.SuiziSensorNum;
import com.antoco.operation.bigscreen.mapper.SuiziSensorNumMapper;
import com.antoco.operation.bigscreen.service.ChannelDataService;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SuiziSensorNumService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/model/suizi-sensor-num")
@RestController
public class SuiziSensorNumControllerImpl implements SuiziSensorNumController {

    @Autowired
    private SuiziSensorNumService suiziSensorNumService;

    @Autowired
    private SuiziSensorNumMapper suiziSensorNumMapper;

    @Autowired
    private ChannelDataService channelDataService;

    @Override
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public SuiziSensorNumDTO save(@Validated @RequestBody SuiziSensorNumDTO suiziSensorNumDTO) {
        SuiziSensorNum suiziSensorNum = suiziSensorNumMapper.asEntity(suiziSensorNumDTO);
        suiziSensorNumService.save(suiziSensorNum);
        channelDataService.pushAllLists(this.getClass().getName());
        return suiziSensorNumDTO;
    }

    @Override
    @GetMapping("/{id}")
    public SuiziSensorNumDTO findById(@PathVariable("id") String id) {
        SuiziSensorNum suiziSensorNum = suiziSensorNumService.findById(id).orElse(null);
        return suiziSensorNumMapper.asDTO(suiziSensorNum);
    }

    @Override
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        suiziSensorNumService.deleteById(id);
        channelDataService.pushAllLists(this.getClass().getName());
    }

    @Override
    @GetMapping
    public List<SuiziSensorNumDTO> list() {
        return suiziSensorNumMapper.asDTOList(suiziSensorNumService.findAll());
    }

    @Override
    @GetMapping("/page-query")
    public Page<SuiziSensorNumDTO> pageQuery(Pageable pageable) {
        Page<SuiziSensorNum> suiziSensorNumPage = suiziSensorNumService.findAll(pageable);
        List<SuiziSensorNumDTO> dtoList = suiziSensorNumPage
                .stream()
                .map(suiziSensorNumMapper::asDTO).collect(Collectors.toList());
        return new PageImpl<>(dtoList, pageable, suiziSensorNumPage.getTotalElements());
    }

    @Override
    @PutMapping("/{id}")
    public SuiziSensorNumDTO update(@RequestBody SuiziSensorNumDTO suiziSensorNumDTO, @PathVariable("id") String id) {
        SuiziSensorNum suiziSensorNum = suiziSensorNumMapper.asEntity(suiziSensorNumDTO);
        suiziSensorNumService.update(suiziSensorNum, id);
        channelDataService.pushAllLists(this.getClass().getName());
        return suiziSensorNumDTO;
    }
}